package com.twitknot;

import info.daveturner.XMLUtils;

import java.util.ArrayList;
import java.util.Iterator;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.apache.commons.lang.StringUtils;


public class TweetParser {
  
  private static final String TWEET_NODE="text";
  private static final String TWEET_ELEMENT="status";
  private static final String HTTP="HTTP://";
  private static final String HTTPS="HTTPS://";
  private static final String WWW=" WWW."; // important to have the empty space before www.
  private static final String EMPTY_SPACE=" ";
  private static final int SANITY_COUNT=30;
  
  ArrayList<String> tweets;
  Document dom;
  
  public TweetParser(Document d) throws Exception{
    dom =d;
    tweets = new ArrayList<String>();
    Element docEle = dom.getDocumentElement();
    
    //get a nodelist of <employee> elements
    NodeList nl = docEle.getElementsByTagName(TWEET_ELEMENT);
    if(nl != null && nl.getLength() > 0) {
      for(int i = 0 ; i < nl.getLength();i++) {
        
        //get the employee element
        Element el = (Element)nl.item(i);
        
        //get the Employee object
        //Employee e = getEmployee(el);
        String s = XMLUtils.getTextValue(el,TWEET_NODE);
        //add it to list
        tweets.add(s);
      }
    }
  }
  
  public ArrayList<String> getTweets()
  {
    return tweets;
  }
  
  public ArrayList<String> getTweetLinks()
  {
    ArrayList<String> links = new ArrayList<String>();
    if(tweets != null && tweets.size() > 0){
      Iterator<String> it = tweets.iterator();
      while(it.hasNext()) {
        ArrayList<String> _links = getLinks(it.next());
        if(_links != null && _links.size() > 0){
          Iterator<String> it2 = _links.iterator();
          while(it2.hasNext()) 
            links.add(it2.next());
        }
      }
    }
    return links;
  }
  
  public ArrayList<String> getLinks(String s)
  {
    ArrayList<String> result = new ArrayList<String>();
    if(StringUtils.isBlank(s))
      return result;
    
    if(!StringUtils.contains(s.toUpperCase(), HTTP) 
        && !StringUtils.contains(s.toUpperCase(), HTTPS)
        && !StringUtils.contains(s.toUpperCase(), WWW))
      return result;
    
    
    appendToArray(result, s, HTTP);
    System.out.println("HTTP:" + result.size());
    appendToArray(result, s, HTTPS);
    System.out.println("HTTPS:" + result.size());
    appendToArray(result, s, WWW);
    System.out.println(" www.:" + result.size());
    
    return result;
  }
  
  private void appendToArray(ArrayList<String> ar, String s, String searchString)
  {
   if(s.toUpperCase().indexOf(searchString) < 0){
      return;
    }
    
    boolean isProcessing = true;
    String _temp = StringUtils.substring(s, s.toUpperCase().indexOf(searchString));
    int sanityCount = 0;
    
    while(isProcessing){
      sanityCount++;
      int emptySpace = StringUtils.substring(_temp, 1).indexOf(EMPTY_SPACE) + 1;
      if(emptySpace < 2){
        emptySpace = _temp.length();
      }
      String toAppend = StringUtils.trim(StringUtils.substring(_temp, 0, emptySpace));
      ar.add(toAppend);
      _temp =StringUtils.substring(_temp, toAppend.length(), _temp.length());
      if(_temp.toUpperCase().indexOf(searchString) < 0){
        isProcessing = false;
        return;
      }else{
        _temp = StringUtils.substring(_temp, _temp.toUpperCase().indexOf(searchString));
      }
      
      if(sanityCount > SANITY_COUNT){
        isProcessing = false;
        return;
      }
    }
  }
}
